import 'package:flutter/cupertino.dart';
import 'package:flutter/material.dart';
import 'package:flutter_base_widget/context_menus/platform_selector.dart';

void main() => runApp(MaterialApp(
      ///默认的首页面
      home: AdaptiveTextSelectionPage(),
    ));

class AdaptiveTextSelectionPage extends StatelessWidget {
  final TextEditingController _controller = TextEditingController(
    text: '程序员如果敲一会就停半天，抱着一杯茶，表情拧巴，那才是在编程',
  );

  @override
  Widget build(BuildContext context) {
    return Scaffold(
      appBar: AppBar(
        title: const Text("输入框选择"),
      ),
      body: Center(
        child: SizedBox(
          width: 300.0,
          child: TextField(
            controller: _controller,
            maxLines: 4,
            minLines: 2,
            contextMenuBuilder:
                (BuildContext context, EditableTextState editableTextState) {
              List<ContextMenuButtonItem> itemList =
                  editableTextState.contextMenuButtonItems;
              return AdaptiveTextSelectionToolbar(
                anchors: editableTextState.contextMenuAnchors,
                children: itemList
                    .map(
                      (bottomItem) => CupertinoButton(
                        color: Colors.amber,
                        padding: EdgeInsets.all(10),
                        disabledColor: Colors.grey,
                        pressedOpacity: 0.7,
                        borderRadius: null,
                        child: SizedBox(child: Text(getBottonLable(bottomItem))),
                        onPressed: bottomItem.onPressed,
                      ),
                    )
                    .toList(),
              );
            },
          ),
        ),
      ),
    );
  }

  String getBottonLable(ContextMenuButtonItem bottomItem) {
    String lable = "";
    switch (bottomItem.type) {
      case ContextMenuButtonType.cut:
        lable = "剪切";
        break;
      case ContextMenuButtonType.copy:
        lable = "复制";
        break;
      case ContextMenuButtonType.paste:
        lable = "粘贴";
        break;
      case ContextMenuButtonType.selectAll:
        lable = "选择全部";
        break;
      case ContextMenuButtonType.custom:
        // TODO: Handle this case.
        break;
    }
    return lable;
  }
}
